package com.example.yuejian.yuejian.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.example.yuejian.yuejian.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.yuejian.yuejian.view.Carousel.setCarousel;

/**
 * A simple {@link Fragment} subclass.
 */
public class TrainFragment extends Fragment implements View.OnClickListener{
    private View view;
    private LinearLayout mTabBird;
    private LinearLayout mTabNew;
    private LinearLayout mTabPromote;
    private LinearLayout mTabAll;

    private TextView tvBird;
    private TextView tvNew;
    private TextView tvPromote;
    private TextView tvAll;

    private Fragment mFmBird;
    private Fragment mFmNew;
    private Fragment mFmPromote;
    private Fragment mFmAll;


    private Context context;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private String[] image = {"company1.jpg","company2.jpg","company3.jpg","company4.jpg"};
    private List<String> images =new ArrayList<>();
    // private List<String> imgUrl =new ArrayList<>();
    // String imgUrl;
    String imgUrl="http://123.207.174.155/zqb/Public/goodsImg/";

    public TrainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_train, container, false);
        initViews();
        initEvents();
        setSelect(0);
        return view;
    }
    private void initViews() {
        mTabBird=(LinearLayout)view.findViewById(R.id.id_tab_train_bird);
        mTabNew=(LinearLayout)view.findViewById(R.id.id_tab_train_new);
        mTabPromote=(LinearLayout)view.findViewById(R.id.id_tab_train_promote);
        mTabAll=(LinearLayout)view.findViewById(R.id.id_tab_train_all);

        tvBird=(TextView)view.findViewById(R.id.id_tab_bird_tv);
        tvNew=(TextView)view.findViewById(R.id.id_tab_new_tv);
        tvPromote=(TextView)view.findViewById(R.id.id_tab_promote_tv);
        tvAll=(TextView)view.findViewById(R.id.id_tab_all_tv);



        for (int i = 0; i < 4; i++) {
            images.add(imgUrl + image[i]);
        }
        context = getActivity();
        li = (LinearLayout) view.findViewById(R.id.play);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        convenient = (ConvenientBanner) view.findViewById(R.id.convenientBanner);
        Display d = getActivity().getWindowManager().getDefaultDisplay();
        //*获取布局参数*//*
        i = li.getLayoutParams();
        i.height = d.getHeight() / 3;
        i.width = d.getWidth();
        setCarousel(context, convenient, images, i.width, i.height);
       /* CarouselModel carouselModel=new  CarouselModel();
        Call<List<Carousel>> call1=carouselModel.Carousel();
        call1.enqueue(new Callback<List<Carousel>>() {
            @Override
            public void onResponse(Call<List<Carousel>> call1, retrofit2.Response<List<Carousel>> response)
            {
                List<Carousel> res=response.body();
                for (int i=0;i<res.size();i++)
                {
                    images.add(res.get(i).getPhoto());
                }
                context=getActivity();
                li=(LinearLayout)view.findViewById(R.id.play);
                // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
                convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
                Display d=getActivity().getWindowManager().getDefaultDisplay();
               *//*获取布局参数*//*
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);

            }
            @Override
            public void onFailure(Call<List<Carousel>> call, Throwable t) {

            }
        });

    }*/
    }
    private void initEvents(){
        mTabBird.setOnClickListener(this);
        mTabNew.setOnClickListener(this);
        mTabPromote.setOnClickListener(this);
        mTabAll.setOnClickListener(this);
    }

    private void resetTextColors() {

        tvBird.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
        tvNew.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
        tvPromote.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
        tvAll.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
    }
    private  void resetBgColors(){
        mTabBird.setBackgroundResource(R.drawable.shape_train_tab);
        mTabNew.setBackgroundResource(R.drawable.shape_train_tab);
        mTabPromote.setBackgroundResource(R.drawable.shape_train_tab);
        mTabAll.setBackgroundResource(R.drawable.shape_train_tab);
        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_train_bird:
                setSelect(0);
                break;
            case R.id.id_tab_train_new:
                setSelect(1);
                break;
            case R.id.id_tab_train_promote:
                setSelect(2);
                break;
            case R.id.id_tab_train_all:
                setSelect(3);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm =getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmBird== null)
                {
                    mFmBird = new TrainBirdFragment();
                    transaction.add(R.id.id_content_train, mFmBird);
                } else
                {
                    transaction.show(mFmBird);
                }
                tvBird.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabBird.setBackgroundResource(R.drawable.shape_train_tab_select);

                break;
            case 1:
                if (mFmNew== null)
                {
                    mFmNew = new TrainNewFragment();
                    transaction.add(R.id.id_content_train, mFmNew);
                } else
                {
                    transaction.show(mFmNew);

                }

                tvNew.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabNew.setBackgroundResource(R.drawable.shape_train_tab_select);

                break;
            case 2:
                if (mFmPromote == null)
                {
                    mFmPromote = new TrainPromoteFragment();
                    transaction.add(R.id.id_content_train, mFmPromote);
                } else
                {
                    transaction.show(mFmPromote);

                }

                tvPromote.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabPromote.setBackgroundResource(R.drawable.shape_train_tab_select);

                break;
            case 3:
                if (mFmAll == null)
                {
                    mFmAll = new TrainAllFragment();
                    transaction.add(R.id.id_content_train, mFmAll);
                } else
                {
                    transaction.show(mFmAll);

                }

                tvAll.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabAll.setBackgroundResource(R.drawable.shape_train_tab_select);

                break;


            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmBird != null)
        {
            transaction.hide(mFmBird);
        }
        if (mFmNew != null)
        {
            transaction.hide(mFmNew);
        }
        if (mFmPromote!= null)
        {
            transaction.hide(mFmPromote);
        }
        if (mFmAll!= null)
        {
            transaction.hide(mFmAll);
        }

    }

}
